﻿using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
namespace WPFExcelReport
{

    public partial class MainViewModel
    {
        public DataSet PlanAB50(DataSet SetEnv, DataSet db, DataRow dwABA,DataRow dwRule)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = INTEXDB.ABPOS.Clone();
            string filter = "";
            string ENDD = "";
            string STARTD = "";
            decimal GLeng = 0;
            DataRow[] rows = null;
            DataRow dwGSDAT = null;
            string krs = "", lfd = "", pos = "";
            string ART = "";
            string VAR = "";
            string WAKZ = "";
            string FARBE = "";
            string FINISH = "";
            decimal FLeng = 0;
            DataRow dwAPZUORD = null;
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataRow dwMU = null;
            double RQTY = 0; 
            try
            {
                #region========Validate=================
                string LFD0 = dwABA["ABAAUFLFD"].ToString();
                if (LFD0.Trim() == "")
                    LFD0 = ABPLFD;
                string POS0 = dwABA["ABAAUFPOS"].ToString();
                if (POS0.Trim()== "")
                {
                    POS0 = ABPOS.ToString(); ABPOS++;
                }
                ART = dwABA["ABAART"].ToString();
                VAR = dwABA["ABAVAR"].ToString();
                WAKZ = dwABA["ABAWAKZ"].ToString();
                FARBE = dwABA["ABAFARBE"].ToString(); 
     
                filter = "GSART='" + ART + "' AND GSVAR='" + VAR + "'";
                rows = db.Tables["GSDAT"].Select(filter);
                foreach (DataRow row in rows)
                {
                    dwGSDAT = row;
                } 
                /*
                if (dwGSDAT["GSFERTKZ"].ToString() != "E"
                    & dwGSDAT["GSFERTKZ"].ToString() != "F"
                    & dwGSDAT["GSFERTKZ"].ToString() != "X"
                    )
                    return SReturn; 
                */
                filter = "APZSTUFE='G' AND APZART='" + ART + "' AND APZVAR='" + VAR + "'  AND APZALFDNR<>'000000' ";
                rows = db.Tables["APZUORD"].Select(filter);
                foreach (DataRow row in rows)
                {
                    dwAPZUORD = row;
                    break;                     
                }
                if (dwAPZUORD == null)
                {
                    APZANRKRS = "RS";
                    APZALFDNR = "000001";
                }
                else
                {
                    APZANRKRS = dwAPZUORD["APZANRKRS"].ToString();
                    APZALFDNR = dwAPZUORD["APZALFDNR"].ToString();
                }
                filter = "APFANRKRS='" + APZANRKRS + "' AND APFALFDNR='" + APZALFDNR + "'";
                rows = db.Tables["APFOLGE"].Select(filter);
                foreach (DataRow row in rows)
                {
                    PRLO = row["KSZLO"].ToString();
                    PRLB = row["KSZLB"].ToString();
                    PRLP = row["KSZLP"].ToString();
                    break;
                }
                #endregion
                #region=====Rules============ 
                QTYEND = 0; QTYMAT = 0; R01 = 0; R02 = 0; F01 = 0;
                if (dwRule != null)
                {
                    R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;
                    R02 = Convert.ToDouble(dwRule["R02"].ToString()) / 100;
                    F01 = Convert.ToDouble(dwRule["F01"].ToString());
                    F02 = Convert.ToDouble(dwRule["F02"].ToString());
                    DELDATE = dwRule["DELDATE"].ToString();
                    QTYEND =Convert.ToDouble(dwRule["DELDATE"].ToString());
                    LDAB = Convert.ToDouble(dwRule["LEADTIME"].ToString());//生产周期
                }
                RQTY = Math.Ceiling(Convert.ToDouble(dwABA["ABARESMG"].ToString()) / 1000);//无放大系数
                QTYEND = Math.Ceiling(RQTY * F02 / F01 / (1 - R02));
                //QTYEND = RQTY;
                if (DELDATE.Trim().Length < 8)
                {
                    ENDD = dwABA["ABAVKSTARTD"].ToString();
                }
                else
                {
                    ENDD = DELDATE;
                }
                STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - LDAB));
                if (STARTD.Trim() == "") STARTD = "0";
                 #endregion
                #region=======dwABPN==========
                dwABPN = tmpABPOS.Rows.Add();
                dwABPN["ABPAENDZL"] =2;
                dwABPN["ABPMANDANT"] = app.Mandant;
                dwABPN["ABPAUFART"] = "GP";
                dwABPN["ABPAUFKRS"] = "JH";
                dwABPN["ABPAUFLFD"] = LFD0;
                dwABPN["ABPAUFPOS"] = POS0;
                dwABPN["ABPNEUBEN"] = app.User;
                dwABPN["ABPNEUDAT"] = date;
                dwABPN["ABPZLO"] = PRLO;
                dwABPN["ABPZLB"] = PRLB; 
                dwABPN["ABPSME"] = "5";
                dwABPN["ABPPOSTYP"] = "JH ";
                dwABPN["ABPWAKZ"] = "G";
                dwABPN["ABPART"] = ART;
                dwABPN["ABPVAR"] = VAR;
                dwABPN["ABPFARBE"] = FARBE;
                dwABPN["ABPLTVORL"] = dwABA["EXART"]; 
                dwABPN["ABPLIEFDAT"] = ENDD;
                dwABPN["ABPVTDATE"] = ENDD;
                dwABPN["ABPENDDAT"] = ENDD;
                dwABPN["ABPENDDATA"] = ENDD;
                dwABPN["ABPLIEFDATN"] = ENDD;
                dwABPN["ABPTERMDAT"] = ENDD;
                dwABPN["ABPSTARTDAT"] = STARTD;
                dwABPN["ABPSTARTDAT"] = STARTD;
                dwABPN["ABPSTARTDATA"] = STARTD;
                dwABPN["ABPSTARTDATN"] = STARTD;
                dwABPN["ABPANRKR"] = APZANRKRS;
                dwABPN["ABPALFDNR"] = APZALFDNR;
                dwABPN["ABPSOLLMENGE1"] = 0;
                dwABPN["ABPSOLLMENGE2"] = 0;
                dwABPN["ABPSOLLMENGE3"] = 0;
                dwABPN["ABPSOLLMENGE4"] = 0;
                dwABPN["ABPSOLLMENGE5"] = QTYEND*100;
                dwABPN["ABPSOLLMENGE6"] = 0;
                dwABPN["ABPSOLLMENGE7"] = 0;
                dwABPN["ABPSOLLMENGE8"] = 0;
                dwABPN["ABPZUGKG"] = 0; 
                #endregion

                #region=======ICC_ABAX ================
                var query = from DataRow row in tbABAUFM.AsEnumerable()
                            where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                            select row;
                foreach (DataRow row in query)
                {
                    row["ABAAUFTYP"] = "V";
                    row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                    row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                    row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                    row["ABAQTYRESERVE"] = RQTY * 10;
                    row["ABARESMG"] = QTYEND * 1000;       
                }
                //产生下层需求
                #region============GAAUF732====================
                DataRow dwGASTGA = null;
                double GAKG1 = 0;
                double RT = 0;
                filter = "GAGART='" + ART + "' AND GAGVAR='" + VAR + "' AND GAGFARBE='" + FARBE + "'";
                rows = db.Tables["GASTGA"].Select(filter);
                if (rows.Length == 0)
                {
                    filter = "GAGART='" + ART + "' AND GAGVAR='" + VAR + "'";
                    rows = db.Tables["GASTGA"].Select(filter);
                }
                string addedpos = "";
                foreach (DataRow row in rows)
                {
                    dwGASTGA = row;
                    string YPOS = dwGASTGA["GAGPOS"].ToString(); 
                     
                    if (addedpos.Contains(YPOS + ";"))
                        continue;
                    addedpos += YPOS + ";";
                    double GAGKGBRUT = Convert.ToDouble(dwGASTGA["GAGKGBRUT"].ToString()) * 0.001 * 0.0001;
                    GAGKGBRUT = 1 + R01;
                    double GABASMG2 = 0;
                    double TargetRatio = 1;
                    GABASMG2 = TargetRatio * GAGKGBRUT;
                    GABASMG2 = 1;
                    if (GABASMG2 == 0)
                    {
                        throw new Exception("请核对纱线工艺!");
                    }
                    GAKG1 = Math.Ceiling(QTYEND * GABASMG2);
                    dwMU = tbABAUFM.Rows.Add();
                    dwMU["ABAID"] = ABAID; ABAID++;
                    #region=====MapRules=======
                    dwMU["QTYEND"] = dwRule["QTYEND"];
                    dwMU["QTYMAT"] = dwRule["QTYMAT"];
                    dwMU["DELDATE"] = dwRule["DELDATE"];
                    dwMU["STARTDATE"] = dwRule["STARTDATE"];
                    dwMU["LEADTIME"] = dwRule["LEADTIME"];
                    dwMU["UNITNUM"] = dwRule["UNITNUM"];
                    dwMU["TTENDS"] = dwRule["TTENDS"];
                    dwMU["UENDS"] = dwRule["UENDS"];
                    dwMU["UNITQTY"] = dwRule["UNITQTY"];
                    dwMU["R01"] = dwRule["R01"];
                    dwMU["R01DES"] = dwRule["R01DES"];
                    dwMU["R02"] = dwRule["R02"];
                    dwMU["R02DES"] = dwRule["R02DES"];
                    dwMU["F01"] = dwRule["F01"];
                    dwMU["F01DES"] = dwRule["F01DES"];
                    dwMU["F02"] = dwRule["F02"];
                    dwMU["F02DES"] = dwRule["F02DES"];
                    dwMU["JNUM"] = dwRule["JNUM"];
                    dwMU["JWASTGE"] = dwRule["JWASTGE"];
                    dwMU["JWASTAGEDES"] = dwRule["JWASTAGEDES"];
                    dwMU["UMS"] = dwRule["UMS"];
                    #endregion
                    dwMU["ABAMANDANT"] = app.Mandant;
                    dwMU["ABAAUFTYP"] = "N";
                    dwMU["ABAVKAUFTYP"] = "V";
                    dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
                    dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
                    dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
                    dwMU["ABAVKUPOS"] = dwGASTGA["GAGPOS"];
                    dwMU["ABPWAKZ"] = "G";
                    dwMU["ABPART"] = ART;
                    dwMU["ABPVAR"] = VAR;
                    dwMU["ABPFARBE"] = FARBE;
                    dwMU["EXART"] = dwABA["EXART"];
                    dwMU["ABAWAKZ"] = "G";
                    dwMU["ABAART"] = dwGASTGA["GAGGART"];
                    dwMU["ABAVAR"] = dwGASTGA["GAGGVAR"];
                    dwMU["ABAFARBE"] = dwGASTGA["GAGGFARBE"];
                    dwMU["ABASSTKLG"] = 0;
                    dwMU["ABARESMG"] = GAKG1*1000;
                    dwMU["ABASANZSTK"] = Math.Ceiling(GAKG1);
                    dwMU["ABASSTKGEW"] = "1000";
                    dwMU["ABAAUFMSL"] = "01"; //GAAUFMSL
                    dwMU["ABAPACKTYP"] = "999 ";
                    dwMU["ABAHUELSE"] = "99    ";
                    dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
                    dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];
                    dwMU["ABABASMG"] = dwGASTGA["GAGBASIS"];
                    dwMU["ABADISPOKZ"] = dwGASTGA["GAGDISPOKZ"];
                    dwMU["ABAVKFARBE"] = Math.Round(GABASMG2 * 1000 * 100);//100000
                    dwMU["ABAQTYALLOCATE"] = 0;
                    dwMU["ABAQTYRECEIVED"] = 0;                     
                    dwMU["ABALO"] = PRLO;
                    dwMU["ABALB"] = PRLB;
                    dwMU["ABALP"] = PRLP;
                    dwMU["ABAUSER"] = app.User;
                    dwMU["ABADATE"] = date; ;
                }
                #endregion
                #endregion
                if (IsAB50)
                {
                    SReturn.Merge(tmpABPOS);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet PlanAB50MU(DataSet SetEnv,DataSet db, DataRow dwABA,string DD)
        {
            string DEND = "0"; int DAYS = 0;
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = INTEXDB.ABPOS.Clone();
            string ENDD = "0";
            string STARTD = "0";
            string ART = "", VAR = "", FARBE = "", WAKZ = "";
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;

            decimal RQTY = 0;
            string VKAUFKRS = "", VKAUFLFD = "", VKAUFPOS = "", ABAVKUPOS = "";
            DataTable tbMU = db.Tables["ICC_ABAX"];
            DataTable tbZU = db.Tables["ZUTEI_ITX"];

            #region========Prepare=================
            string LFD0 = dwABA["ABAAUFLFD"].ToString();
            if (LFD0.Trim() == "")
                LFD0 = ABPLFD;
            string POS0 = dwABA["ABAAUFPOS"].ToString();
            if (POS0.Trim() == "")
            {
                POS0 = ABPOS.ToString(); ABPOS++;
            }
            VKAUFKRS = dwABA["ABAVKAUFKRS"].ToString();
            VKAUFLFD = dwABA["ABAVKAUFLFD"].ToString();
            VKAUFPOS = dwABA["ABAVKAUFPOS"].ToString();
            ABAVKUPOS = dwABA["ABAVKUPOS"].ToString();
            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();
            APZANRKRS = "LS";
            APZALFDNR = "000001";
            RQTY = Convert.ToDecimal(dwABA["ABARESMG"].ToString())/10;
            if (DEND == "0")
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DEND;
            }
            STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - DAYS));
            if (STARTD.Trim() == "") STARTD = "0";
            #endregion

            #region=======dwABPN==========
            dwABPN = tmpABPOS.Rows.Add();
            dwABPN["ABPMANDANT"] = app.Mandant;
            dwABPN["ABPAUFART"] = "GP";
            dwABPN["ABPAUFKRS"] = "LS";
            dwABPN["ABPAUFLFD"] = LFD0;
            dwABPN["ABPAUFPOS"] = POS0;
            dwABPN["ABPLTVORL"] = Helpers.Helper.Left(dwABA["EXART"].ToString(), 15);
            dwABPN["ABPZLO"] = dwABA["ABALO"];
            dwABPN["ABPZLB"] = dwABA["ABALB"];
            dwABPN["ABPBLO"] = dwABA["ABALO"];

            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPPOSTYP"] = "MU ";
            dwABPN["ABPWAKZ"] = "G";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPVTDATE"] = ENDD;
            dwABPN["ABPENDDAT"] = ENDD;
            dwABPN["ABPENDDATA"] = ENDD;
            dwABPN["ABPLIEFDATN"] = ENDD;
            dwABPN["ABPTERMDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDATA"] = STARTD;
            dwABPN["ABPSTARTDATN"] = STARTD;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSOLLMENGE1"] = 0;
            dwABPN["ABPSOLLMENGE2"] = 0;
            dwABPN["ABPSOLLMENGE3"] = 0;
            dwABPN["ABPSOLLMENGE4"] = 0;
            dwABPN["ABPSOLLMENGE5"] = RQTY / 10;
            dwABPN["ABPSOLLMENGE6"] = 0;
            dwABPN["ABPSOLLMENGE7"] = 0;
            dwABPN["ABPSOLLMENGE8"] = 0;
            dwABPN["ABPZUGKG"] = RQTY / 10;
            SReturn.Merge(tmpABPOS);
            #endregion

            #region=======ICC_ABAX ================
            var qMU = from DataRow row in tbMU.AsEnumerable()
                      where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                      select row;
            foreach (DataRow row in qMU)
            {
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                //row["ABAQTYRESERVE"] = dwABA["ABARESMG"];
            }
            DataTable tb = qMU.CopyToDataTable();
            tb.TableName = "ICC_ABAX";
            //SReturn.Merge(tb);
            #endregion
 
            #region======Material===========
            DataRow dwABAN = tbMU.Rows.Add();
            dwABAN["ABAID"] = ABAID++;
            dwABAN["ABAMANDANT"] = dwABPN["ABPMANDANT"];
            dwABAN["ABAAUFTYP"] = "N";
            dwABAN["ABAVKAUFTYP"] = "V";
            dwABAN["ABAAUFKRS"] = "00";
            dwABAN["ABAAUFLFD"] = "000000";
            dwABAN["ABAAUFPOS"] = "00";
            dwABAN["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
            dwABAN["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
            dwABAN["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
            dwABAN["ABAVKUPOS"] = "01";
            if (dwABAN["ABAVKUPOS"].ToString().Trim() == "00")
            {
                dwABAN["ABAVKUPOS"] = "01";
            }
            dwABAN["ABAAUFMSL"] = dwABA["ABAAUFMSL"]; //GAAUFMSL
            dwABAN["ABAPACKTYP"] = dwABA["ABAPACKTYP"];
            dwABAN["ABAHUELSE"] = dwABA["ABAHUELSE"];
            dwABAN["ABARESMG"] = dwABA["ABARESMG"];
            dwABAN["ABASANZSTK"] = dwABA["ABASANZSTK"];
            dwABAN["ABASSTKGEW"] = dwABA["ABASSTKGEW"];
            dwABAN["ABAWAKZ"] = dwABA["ABAWAKZ"];
            dwABAN["ABAART"] = dwABA["ABAART"];
            dwABAN["ABAVAR"] = dwABA["ABAVAR"];
            dwABAN["ABAFARBE"] = dwABA["ABAFARBE"];
            dwABAN["ABAVKSTARTD"] = dwABA["ABAVKSTARTD"];
            dwABAN["ABAQTYALLOCATE"] = 0;
            dwABAN["ABAQTYRECEIVED"] = 0;    
            dwABAN["ABADISPOKZ"] = "B";
            dwABAN["ABALO"] = dwABA["ABALO"];
            dwABAN["ABALB"] = dwABA["ABALB"];
            dwABAN["ABALP"] = dwABA["ABALP"];
            dwABAN["ABAUSER"] = app.User;
            dwABAN["ABADATE"] = date;
            string filter = "ABAID='" + dwABAN["ABAID"].ToString() + "'";
            DataRow[] rows = tbMU.Select(filter);
            tb = rows.CopyToDataTable();
            tb.TableName = "ICC_ABAX";
            SReturn.Merge(tb);
            #endregion
            return SReturn;
        }
    }
}